#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


# In[3]:


data1=pd.DataFrame({'X':np.random.randint(1,50,100),'Y':np.random.randint(1,50,100)})


# In[5]:


data=pd.concat([data1+50,data1])


# In[11]:


plt.style.use('ggplot')  #绘制一张网格图
plt.scatter(data.X,data.Y)


# In[13]:


from sklearn.cluster import KMeans


# In[25]:


y_pred=KMeans(n_clusters=2).fit_predict(data) #分成2类


# In[26]:


y_pred


# In[27]:


plt.scatter(data.X,data.Y,c=y_pred)


# In[28]:


from sklearn import metrics                 #引入包
metrics.calinski_harabaz_score(data,y_pred) #评价k聚类好坏，值越大越好


# In[29]:


y_pred=KMeans(n_clusters=3).fit_predict(data) #分成3类进行聚类


# In[30]:


plt.scatter(data.X,data.Y,c=y_pred)


# In[31]:


metrics.calinski_harabaz_score(data,y_pred) #（数据，预测的值）进行评分 


# DBSCAN算法

# DBSCAN有两个重要参数，eps是邻域半径，min_samples是密度阈值

# In[34]:


from sklearn.cluster import DBSCAN


# In[63]:


y_pred=DBSCAN(eps=8).fit_predict(data)


# In[64]:


plt.scatter(data.X,data.Y,c=y_pred)


# In[67]:


get_ipython().run_line_magic('pinfo', 'DBSCAN')


# In[ ]:




